<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Sys_Role;
use App\Services\UserService;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;

class UserController extends Controller
{
    public function userAuth()
    {
        $user = JWTAuth::parseToken()->authenticate();
        $arr = json_decode($user->hasOneRole->permissions,true)['permissions'];

        $user->permissions = $this->convertTree($arr);
        $user = $user->makeHidden(['hasOneRole']);
        return $this->responseSuccess($user);
    }

    public function getUserList(UserService $userService , Request $request)
    {
        $params = $request->input();
        $list = $userService->getUserList($params);
        return $this->responseSuccess($list);
    }

    public function userList(UserService $userService , Request $request)
    {
        $params = $request->input();
        $list = $userService->getList($params);
        return $this->responseSuccess( $list['data']);
    }

    public function userAdd(UserService $userService , Request $request)
    {
        $params = $request->input();
        $list = $userService->addUser($params);
        return $this->responseSuccess($list['data']);
    }

    public function userDel(UserService $userService , Request $request)
    {
        $params = $request->input();
        $params['mec_id'] = $request->user()->token()->mec_id;
        $list = $userService->delUser($params);
        return $this->responseSuccess( $list['data']);
    }
}
